﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.IO;

namespace ETRoboConTrackingSystemServer
{
    public partial class Main : System.Web.UI.Page
    {
        private Dictionary<string, int> dbContests = new Dictionary<string, int>();
        private Dictionary<string, int> dbAreas = new Dictionary<string, int>();
        private Dictionary<string, int> dbGameSpecs = new Dictionary<string, int>();

        protected void Page_Load(object sender, EventArgs e)
        {
            RefleshContestsAndGameSpecs();
        }

        protected void ButtonDefFileUpload_Click(object sender, EventArgs e)
        {
            if (File.Exists(TextBoxDefFileName.Text))
            {
                TextReader reader = new StreamReader(new FileStream(TextBoxDefFileName.Text, FileMode.Open, FileAccess.Read));
                string defFileContent = reader.ReadToEnd();
                
            }
            RefleshContestsAndGameSpecs();
        }

        private void RefleshContestsAndGameSpecs()
        {
            dbContests.Clear();
            DropDownListContest.Items.Clear();
            dbGameSpecs.Clear();
            DropDownListGameSpec.Items.Clear();
            using (var db = new ETRCGoVADBEntities())
            {
                var contests = from c in db.ContestSet select c;
                foreach (var contest in contests)
                {
                    dbContests.Add(contest.Name, contest.ContestId);
                    DropDownListContest.Items.Add(contest.Name);
                }
                var gameSpecs = from gs in db.GameSpecSet select gs;
                foreach (var gameSpec in gameSpecs)
                {
                    dbGameSpecs.Add(gameSpec.Name,gameSpec.GameSpecId);
                    DropDownListGameSpec.Items.Add(gameSpec.Name);
                }
            }
        }

        protected void ButtonCreateGame_Click(object sender, EventArgs e)
        {
            int selectedContestId;
            int selectedAreaId;
            int selectedGameSpecId;
            if (dbContests.TryGetValue(DropDownListContest.SelectedItem.Text, out selectedContestId)
                && dbAreas.TryGetValue(DropDownListArea.SelectedItem.Text, out selectedAreaId)
                && dbGameSpecs.TryGetValue(DropDownListGameSpec.SelectedItem.Text, out selectedGameSpecId)
                && string.IsNullOrEmpty(TextBoxGameName.Text) == false)
            {
                using (var db = new ETRCGoVADBEntities())
                {
                    AreaSet area = null;
                    GameSpecSet gameSpec = null;

                    var areas = from a in db.AreaSet where a.AreaId == selectedAreaId select a;
                    if (areas.Count() == 1)
                    {
                        area = areas.First();
                    }
                    var gameSpecs = from gs in db.GameSpecSet where gs.GameSpecId == selectedGameSpecId select gs;
                    if (gameSpecs.Count() == 1)
                    {
                        gameSpec = gameSpecs.First();
                    }
                    if (area != null && gameSpec != null && area.Contest_ContestId == selectedContestId)
                    {
                        var games = from g in db.GameSet
                                    where g.Contest_ContestId == selectedContestId
                                    && g.Area_AreaId == selectedAreaId
                                    && g.Name.CompareTo(TextBoxGameName.Text)==0
                                    select g;
                        if (games.Count() == 0)
                        {
                            var game = new GameSet
                           {
                               Name = TextBoxGameName.Text,
                               AreaSet = area,
                               GameSpecSet = gameSpec,
                               ContestSet = area.ContestSet
                           };
                            db.AddToGameSet(game);
                            db.SaveChanges();
                            TextBoxGameId.Text = game.GameId.ToString();
                        }
                        else
                        {
                            var game = games.First();
                            game.ContestSet = area.ContestSet;
                            game.AreaSet = area;
                            game.GameSpecSet = gameSpec;
                            db.SaveChanges();
                        }
                    }
                }
            }
        }

        protected void DropDownListContest_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectedContestId;
            if (dbContests.TryGetValue(DropDownListContest.SelectedItem.Text, out selectedContestId))
            {
                dbAreas.Clear();
                DropDownListArea.Items.Clear();
                using (ETRCGoVADBEntities db = new ETRCGoVADBEntities())
                {
                    var areas = from a in db.AreaSet where a.Contest_ContestId==selectedContestId select a;
                    foreach (var area in areas)
                    {
                        DropDownListArea.Items.Add(area.Name);
                        dbAreas.Add(area.Name, area.AreaId);
                    }
                }
            }
        }

        protected void DropDownListArea_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectedAreaId;
            if (dbAreas.TryGetValue(DropDownListArea.SelectedItem.Text, out selectedAreaId))
            {
                using (ETRCGoVADBEntities db = new ETRCGoVADBEntities())
                {
                    var games = from g in db.GameSet where g.Area_AreaId == selectedAreaId select g;
                    if (games.Count() == 1)
                    {
                        var game = games.First();
                        TextBoxGameId.Text = game.GameId.ToString();
                        if (game.GameSpecSet != null)
                        {
                            DropDownListGameSpec.Text = game.GameSpecSet.Name;
                            TextBoxGameName.Text = game.Name;
                        }
                    }
                }
            }
        }

        protected void ButtonSelectFile_Click(object sender, EventArgs e)
        {
            
        }
    }
}